package com.tqjc.system.core.init;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tqjc.area.entity.SysConfigEntity;
import com.tqjc.area.service.SysConfigService;
import com.tqjc.execute.consts.WorkConst;
import com.tqjc.execute.utils.StaticMethod;
import com.tqjc.system.common.constant.ConfigConstant;
import com.tqjc.system.common.constant.GlobalConstant;
import com.tqjc.system.core.threadpool.ThreadPoolManager;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

import com.tqjc.system.system.service.SystemService;

import lombok.extern.slf4j.Slf4j;

import javax.annotation.Resource;

/**
 * 初始化启动执行
 * @author teli_
 *
 */
@Slf4j
@Configuration
public class CommonStartConfig implements InitializingBean, DisposableBean {

	@Resource(name = "systemService")
	private SystemService systemService;

	@Autowired
	private SysConfigService sysConfigService;
	
	/**
	 * spring初始化执行
	 */
	@Override
	public void afterPropertiesSet() throws Exception {
		log.info("---------------初始化启动执行---------------");
		
		//系统初始化数据字典
		systemService.initDictionary();
		//系统初始化部门数据
		systemService.initDeparts();

		initConfig();
	
	}

	private void initConfig() {

		SysConfigEntity sysConfigEntity = sysConfigService.getOne(new LambdaQueryWrapper<SysConfigEntity>()
				.eq(SysConfigEntity::getConfigId, ConfigConstant.CONFIG_ID_MULT_STEEL_WAIT_SECOND)
				.eq(SysConfigEntity::getConfigType, ConfigConstant.CONFIG_TYPE_TIME).eq(SysConfigEntity::getConfigStatus,"1"));
		if(sysConfigEntity != null){
			GlobalConstant.MULT_STEEL_WAIT_SECOND = Integer.valueOf(sysConfigEntity.getConfigValue());
		}

		SysConfigEntity octConfigEntity = sysConfigService.getOne(new LambdaQueryWrapper<SysConfigEntity>()
				.eq(SysConfigEntity::getConfigId, ConfigConstant.CONFIG_ID_OCT_FLAG)
				.eq(SysConfigEntity::getConfigType, ConfigConstant.CONFIG_TYPE_FLAG).eq(SysConfigEntity::getConfigStatus,"1"));
		if(octConfigEntity != null){
			StaticMethod.SPECIAL_STEEL_NO_OCT = octConfigEntity.getConfigValue();
		}
		SysConfigEntity novConfigEntity = sysConfigService.getOne(new LambdaQueryWrapper<SysConfigEntity>()
				.eq(SysConfigEntity::getConfigId, ConfigConstant.CONFIG_ID_NOV_FLAG)
				.eq(SysConfigEntity::getConfigType, ConfigConstant.CONFIG_TYPE_FLAG).eq(SysConfigEntity::getConfigStatus,"1"));
		if(novConfigEntity != null){
			StaticMethod.SPECIAL_STEEL_NO_NOV = novConfigEntity.getConfigValue();
		}
		SysConfigEntity decConfigEntity = sysConfigService.getOne(new LambdaQueryWrapper<SysConfigEntity>()
				.eq(SysConfigEntity::getConfigId, ConfigConstant.CONFIG_ID_DEC_FLAG)
				.eq(SysConfigEntity::getConfigType, ConfigConstant.CONFIG_TYPE_FLAG).eq(SysConfigEntity::getConfigStatus,"1"));
		if(decConfigEntity != null){
			StaticMethod.SPECIAL_STEEL_NO_DEC = decConfigEntity.getConfigValue();
		}

		SysConfigEntity no2DebugFlagEntity = sysConfigService.getOne(new LambdaQueryWrapper<SysConfigEntity>()
				.eq(SysConfigEntity::getConfigId, ConfigConstant.CONFIG_ID_NO2_DEBUG_FLAG)
				.eq(SysConfigEntity::getConfigType, ConfigConstant.CONFIG_TYPE_FLAG).eq(SysConfigEntity::getConfigStatus,"1"));

		if(no2DebugFlagEntity != null){
			WorkConst.CRANE_DEBUG_SWITCH_FLAG = Boolean.valueOf(no2DebugFlagEntity.getConfigValue());
		}

	}


	/**
	 * 容器销毁执行
	 */
	@Override
	public void destroy() throws Exception {
		log.info("---------------容器销毁启动执行---------------");
		
		//关闭线程池
		ThreadPoolManager.getInstance().shutdown();
		
		log.info("---------------容器销毁启动执行完成---------------");
	}
	

}
